<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\controller;
use app\common\model\EthAccount;
use app\common\model\UserAccount;
use app\common\model\UserPayType;
use think\Db;
/**
 * 用户接口控制器
 */
class User extends ApiBase
{

    /**
     * 获取用户信息接口
     */
    public function get_userinfo()
    {

        return $this->apiReturn($this->logicUser->getuserinfo($this->param));
    }

    /**
     * 修改用户信息接口
     */
    public function up_userinfo()
    {

        return $this->apiReturn($this->logicUser->up_userinfo($this->param));
    }

    /**
     * 验证是否禁止游戏
     */
    public function is_game()
    {

        return $this->apiReturn($this->logicUser->is_game($this->param));
    }

    /**
     * 获取实名信息
     */
    public function get_idcard()
    {

        return $this->apiReturn($this->logicUser->get_idcard($this->param));
    }



    /**
     *支付方式开关
     */
    public function pay_switch()
    {

        return $this->apiReturn($this->logicUser->pay_switch($this->param));
    }

    /**
     * 验证手机验证码接口
     */
    public function check_mobile(){
        return $this->apiReturn($this->logicUser->check_mobile($this->param));
    }

    /**
     * 添加支付银行卡
     */
    public function add_bank()
    {

        return $this->apiReturn($this->logicUser->add_bank($this->param));
    }

    /**
     * 添加支付银行卡
     */
    public function get_balance()
    {
        $type=$this->param['type'];
        $wallet=\app\common\model\Wallet::get($this->param['wallet_id']);
        if($type==1){

            $info=  UserAccount::get(['user_id'=>$this->logicApiBase->member['id'],'wallet_id'=>$this->param['wallet_id']]);
            $balance=$info['balance'];
            $total=format_money( $balance*$wallet['parities'] );
            $lock=$info['freeze_balance'];
        }else{
           $do=$wallet['symbol'].'_do';
           $lock=$wallet['symbol'].'_lock';
            $info=EthAccount::get([ 'user_id'=>$this->logicApiBase->member['id'] ]);
            $balance=$info[$do];
            $lock=$info[$lock];
            $total=format_money( ($balance+$lock)*$wallet['parities'] );
        }
        return $this->apiReturn(['balance'=>$balance,'lock'=>$lock,'total'=>$total]);
    }
    /**
     * 修改支付银行卡
     */
    public function edit_bank()
    {

        return $this->apiReturn($this->logicUser->edit_bank($this->param));
    }

    /**
     * 获取微信账户支付宝账户
     */
    public function get_pay_name()
    {
        $uid=decoded_user_token($this->param['user_token'],'member_id');

        $name=UserPayType::where(['user_id'=>$uid,'status'=>1,'type'=>$this->param['type']])->value('bank_card');

        return $this->apiReturn($name);
    }

    /**
     * 获取币种地址
     */
    public function get_pay_address()
    {
        $uid=decoded_user_token($this->param['user_token'],'member_id');

        $name=UserPayType::where(['user_id'=>$uid,'status'=>1,'type'=>4,'wallet_id'=>$this->param['wallet_id']])->value('wallet_address');

        return $this->apiReturn($name);
    }
    /**
     * 解绑银行卡
     */
    public function del_bank()
    {

        return $this->apiReturn($this->logicUser->del_bank($this->param));
    }
    /**
     *获取支付方式列表接口
     */
    public function get_paytype_list()
    {

        return $this->apiReturn($this->logicUser->get_paytype_list($this->param));
    }

    /**
     *重置密码，重置支付密码接口
     */
    public function reset_password()
    {

        return $this->apiReturn($this->logicUser->reset_password($this->param));
    }

    /**
     *获取区块链地址列表接口
     */
    public function get_address_list()
    {

        return $this->apiReturn($this->logicUser->get_address_list($this->param));
    }

    /**
     *获取提现方式列表接口
     */
    public function get_user_pay_list()
    {

        return $this->apiReturn($this->logicUser->get_user_pay_list($this->param));
    }

    /**
     *获取用户收款方式详情接口
     */
    public function get_user_pay_info()
    {

        return $this->apiReturn($this->logicUser->get_user_pay_info($this->param));
    }

    /**
     * 支付方式详情接口
     */
    public function get_paytype_info()
    {

        return $this->apiReturn($this->logicUser->get_paytype_info($this->param));
    }
    /**
     * 用户订单接口
     */
    public function order_list()
    {

        return $this->apiReturn($this->logicUser->order_list($this->param));
    }

    /**
     * 新OTC订单接口
     */
    public function new_otc_order_list()
    {

        return $this->apiReturn($this->logicUser->new_otc_order_list($this->param));
    }


    /**
     * 用户新财务明细接口
     */
    public function new_order_list()
    {

        return $this->apiReturn($this->logicUser->new_order_list($this->param));
    }


    /**
     * 验证接口
     */
    public function check_user(){
        $data=$this->param;
        if($data['type']==1){
            if(Db::name('member')->where(['username'=>$data['username']])->count() >0){
                return $this->apiReturn( 0);;
            }else{
                return $this->apiReturn( 1);
            }

        }
        if($data['type']==2){
            if(Db::name('member')->where(['mobile'=>$data['mobile'],'area'=>$data['area']])->count() >0){
                return $this->apiReturn( 0);
            }else{
                return $this->apiReturn( 1);
            }
        }
        if($data['type']==3){
            if(Db::name('member')->where(['user_email'=>$data['user_email']])->count() >0){
                return $this->apiReturn( 0);
            }else{
                return $this->apiReturn( 1);
            }
        }
    }


    /***
     * 记录用户活动信息
     */
    public function last_login_user(){
        $data=$this->param;
        $uid=decoded_user_token($data['user_token'],'member_id');
        $ip=get_client_ip();
        if(empty($data['last_login_area'])){

            $info=getCity($ip);
            if($info){
                $data['last_login_area']=$info['country'].'-'.$info['region'].'-'.$info['city'];
            }

        }
        Db::name('member')->update(['id'=>$uid,'last_login_time'=>time(),'last_login_area'=>$data['last_login_area'],'device_number'=>$data['device_number'],'last_login_ip'=>$ip]);
        return $this->apiReturn( 0);
    }


    /**
     * 组织图接口
     */
    public function organization(){
        return $this->apiReturn($this->logicUser->organization($this->param));
    }

    /**
     * 查看下级接口
     */
    public function share_user(){
        return $this->apiReturn($this->logicUser->share_user($this->param));
    }


    /**
     * 用户详情接口
     */
    public function user_detail(){
        return $this->apiReturn($this->logicUser->user_detail($this->param));
    }

    /**
     * 用户详情接口
     */
    public function user_trade(){
        return $this->apiReturn($this->logicUser->user_trade($this->param));
    }

    /**
     * 设置返点接口
     */
    public function set_point(){
        return $this->apiReturn($this->logicUser->set_point($this->param));
    }


    /**
     * 精准开户接口
     */
    public function account_opening(){
        return $this->apiReturn($this->logicUser->account_opening($this->param));
    }



    /**
     * 用户反馈接口
     */
    public function feedback(){
        return $this->apiReturn($this->logicUser->feedback($this->param));
    }


    //获取虚拟币支付地址
    public function address(){
        $currency_id = input("currency_id");
        $token = input("user_token");
        $uid = decoded_user_token($token,'member_id');
        $address = Db::name("user_account")->where(["user_id"=>$uid,"wallet_id"=>$currency_id])->find()['blockchain_address2'];
        return $this->apiReturn($address);
    }
    /**
     * 高级认证接口
     */
    public function advertiser_attestation(){
        return $this->apiReturn($this->logicUser->advertiser_attestation($this->param));
    }
    /**
     * 高级认证详情接口
     */
    public function advertiser_attestation_info(){
        return $this->apiReturn($this->logicUser->advertiser_attestation_info($this->param));
    }
    /**
     * 发布买卖单接口
     */
    public function release_advertisement(){
        return $this->apiReturn($this->logicUser->release_advertisement($this->param));
    }

    /**
     * 消息通知接口
     */
    public function user_notice_list(){
        return $this->apiReturn($this->logicUser->user_notice_list($this->param));
    }

    /**
     * 消息通知详情接口
     */
    public function user_notice_info(){
        return $this->apiReturn($this->logicUser->user_notice_info($this->param));
    }

    /**
     * 搜索用户接口
     */
    public function get_user(){
        return $this->apiReturn($this->logicUser->get_user($this->param));
    }
    /**
     * 发放幸运奖接口
     */
    public function lucky_reward(){
        return $this->apiReturn($this->logicUser->lucky_reward($this->param));
    }

    /**
     * 下注通知接口
     */
    public function bets_log(){
        return $this->apiReturn($this->logicUser->bets_log($this->param));
    }

    /**
     * 游戏金币转入总包接口
     */
    public function transfer_to_wallet(){
        return $this->apiReturn($this->logicUser->transfer_to_wallet($this->param));
    }

    /**
     * 绑定推荐人接口
     */
    public function blind_leader(){
        return $this->apiReturn($this->logicUser->blind_leader($this->param));
    }

    /**
     *接口
     */
    public function profit_reward(){
        return $this->apiReturn($this->logicUser->profit_reward($this->param));
    }

    /**
     * 用户消息弹窗接口
     */
    public function user_popup(){
        return $this->apiReturn($this->logicUser->user_popup($this->param));
    }


    /**
     * 购买会员卡接口
     */
    public function buy_member_card(){
        return $this->apiReturn($this->logicUser->buy_member_card($this->param));
    }

    /**
     * 应用余额转账接口
     */
    public function app_balance_transfer(){
        return $this->apiReturn($this->logicUser->app_balance_transfer($this->param));
    }

    /**
     * 应用余额转账记录接口
     */
    public function app_balance_transfer_log(){
        return $this->apiReturn($this->logicUser->app_balance_transfer_log($this->param));
    }
    /**
     *消息已读接口
     */
    public function read_notice(){
        return $this->apiReturn($this->logicUser->read_notice($this->param));
    }

    /**
     * 获取谷歌密钥
     */
    public function get_google_account(){
        include_once(ROOT_PATH . 'extend/GoogleAuthenticator/PHPGangsta/GoogleAuthenticator.php');
        $ga = new \PHPGangsta_GoogleAuthenticator();
        $secret = $ga->createSecret();   
        $qrCodeUrl = $ga->getQRCodeGoogleUrl('ccie', $secret, 'googleVerify');
        $result=array();
        $result['secret'] = $secret;
        $result['qrCodeUrl']=$qrCodeUrl;
        echo json_encode(ajaxreturn(0, '获取成功',$result));exit;
    }

    /**
     * 绑定谷歌动态码
     */
    public function bind_google_accout(){
        $token = input("user_token");
        $uid = decoded_user_token($token,'member_id');
        $member = Db::name("member")->where(['id'=>$uid])->find();
        if(data_md5_key($this->param['password']) !== $member['password']){
            echo json_encode(ajaxreturn(1, '密码错误'));exit;
        }
        if(empty($this->param['secret'])){
            echo json_encode(ajaxreturn(1, '密钥不得为空'));exit;
        }
        if(empty($this->param['auth_code'])){
            echo json_encode(ajaxreturn(1, '谷歌验证码不得为空'));exit;
        }
        include_once(ROOT_PATH . 'extend/GoogleAuthenticator/PHPGangsta/GoogleAuthenticator.php');
        $ga = new \PHPGangsta_GoogleAuthenticator();
        $checkResult = $ga->verifyCode($this->param['secret'], $this->param['auth_code'], 1); 
        if (!$checkResult) {
            echo json_encode(ajaxreturn(1, '谷歌验证码错误'));exit;
        } else {
            $res=Db::name("member")->where(['id'=>$uid])->update(["google_auth"=>$this->param['secret']]);
            if(!$res){
                echo json_encode(ajaxreturn(1, '绑定失败'));exit;
            }else{
                echo json_encode(ajaxreturn(0, '绑定成功'));exit;
            }
        }
        
    }

    /**
     * 开启关闭谷歌验证
     */
    public function google_auth_switch(){
        $token = input("user_token");
        $uid = decoded_user_token($token,'member_id');
        $member = Db::name("member")->where(['id'=>$uid])->find();
        if($member['google_open']){
            $status = 0;
        }else{
            $status = 1;
        }
        $res = Db::name("member")->where(["id"=>$uid])->update(["google_open"=>$status]);
        if(!$res){
            echo json_encode(ajaxreturn(1, '切换失败'));exit;
        }else{
            echo json_encode(ajaxreturn(0, '切换成功'));exit;
        }
    }

    

    //用户采集数字货币订单
    public function add_coin_order(){
        $token = input("user_token");
        $uid = decoded_user_token($token,'member_id');
        $result = Db::name("user_account")->where(["user_id"=>$uid])->where("wallet_id in(6,7,8)")->select();
        foreach($result as $k => $v){
            $address = $v['blockchain_address'];//钱包地址
            $wallet_id = $v['wallet_id'];//钱包ID 6 ECT 7 BTC 8 USDT
            $wallet = Db::name("wallet")->where(["id"=>$wallet_id])->find();
            $symbol = $wallet['symbol'];//标志
            $rpc_host = $wallet['rpc_host'];//节点地址
            $datas = array();
            Db::startTrans();
            try{
                //取交易记录
                $coin_collect_order = coin_collect_order($symbol,$address,$wallet_id);
              //  dump($coin_collect_order);
                $finance_account = Db::name("user_account")->where(["user_id"=>$uid])->find();
                foreach($coin_collect_order as $coin_collect_order_v){
                    $number = $coin_collect_order_v['number'];
                    $order = $coin_collect_order_v['tx'];
                    $from = $coin_collect_order_v['from'];
                    $to = $coin_collect_order_v['to'];

                    //判断该订单是否已有
                    $count = Db::name("transfer")->where("serial_number",$order)->count();
                    if($count){
                        continue;
                    }
                    //充值订单
                    $data['form_wallet_id'] = $wallet_id;
                    $data['user_id'] = $uid;
                    $data['type'] = 4;
                    $data['hash'] = $order;
                    $data['from'] = $from;
                    $data['to'] = $to;
                    $data['number'] = $number;
                    $data['create_time'] = time();
                    $data['update_time'] = time();
                    $data['state'] = 0;
                    $data['is_coin'] = 1;
                    $data['serial_number'] = $order;
                    $datas[] = $data;
                }

                if(!empty($datas)){
                    Db::name("transfer")->insertAll($datas);
                    $datas = array();
                }
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                //dump($e);
                //回滚事务
                Db::rollback();
            }
        }
    }
}
